/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package towerz.states;

import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import towerz.component.Button;
import towerz.system.StateManager;
import towerz.system.StringManager;

/**
 *
 * @author Knight
 */
public class StatePause extends GameState{	
    public StatePause(){
		this(-1);
	}
	
	public StatePause(int score) {
		super(GameState.PAUSE);

		//Initialize
		m_Drawn		= false;
		m_Selected 	= 0;
		m_Score		= score;
        
		initButtons();
	}

	private void initButtons(){
		//Create buttons
		m_Buttons = new Vector();

		m_Buttons.addElement(new Button(1, "/button-level.png", 70,  70, 3, StringManager.instance().getString("resume"))); //Resume
        m_Buttons.addElement(new Button(1, "/button-level.png", 70,  110, 3, StringManager.instance().getString("main"))); //Back to main menu
	}

	public void keyPressed(int key){
		//Super
		super.keyPressed(key);

		if (key == -7 || key == 35) { //Is right pressed?
			m_Active = false;
		}
	}

	public void update(long time, int keys) {
		//Super
		super.update(time, keys);
        
		//Change selection based on key
		if ((keys & GameCanvas.UP_PRESSED) != 0)	m_Selected--;
		if ((keys & GameCanvas.DOWN_PRESSED) != 0)	m_Selected++;

		//Correct selection
		if (m_Selected < 0) m_Selected = m_Buttons.size() - 1;
		m_Selected = m_Selected % m_Buttons.size();

		//If button is pressed
		if ((keys & GameCanvas.FIRE_PRESSED) != 0) { //Goto GAME state
			((Button)m_Buttons.elementAt(m_Selected)).setState(Button.STATE_PRESSED);

			//Based on the currently selected
			switch(m_Selected){
                case 0: //Resume
                    m_Active = false;
                    break;
                case 1: //Back to main menu
					if (m_Score >= 0) {
						Vector Parameters = new Vector();
						Parameters.addElement(new Integer(StateHighScore.SAVE_REQUEST));
						Parameters.addElement(new Integer(m_Score));

						StateManager.instance().goTo(GameState.HIGH_SCORE, Parameters, true);
					} else StateManager.instance().goTo(GameState.FRONT, null, false);
                    break;
            }
		}

		//Restart state
		for (int i = 0; i < m_Buttons.size(); i++) {
			((Button)m_Buttons.elementAt(i)).setState(Button.STATE_NONE);
		}

		//Manage the selected button state
		((Button)m_Buttons.elementAt(m_Selected)).setState(Button.STATE_SELECTED);
	}

	public void draw(Graphics g) {
		//Super
		//super.draw(g);
		
		//Draw dark once
		if (!m_Drawn) g.drawImage(StateManager.instance().getDarkImage(), 0, 0, Graphics.LEFT | Graphics.TOP);
		m_Drawn = true;
		
		//Draw buttons
		for (int i = 0; i < m_Buttons.size(); ++i) {
			((Button)m_Buttons.elementAt(i)).draw(g);
		}
	}

	//DATA
	protected int		m_Selected;
	protected int		m_MaxLevel;
	protected Vector	m_Buttons;
	protected boolean	m_Drawn;
	protected int		m_Score;
}
